<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <script src="crypto.js"></script>
    <script src="sha256.js"></script>
    <script>
var defaultOptions = {
  PSW_LENGTH: 8
};

chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
  var res = messageHandler[request.type](request, sender);
  sendResponse(res);
});

var messageHandler = {
  requestPassword: function(request, sender) {
    var url = "infobar.html#tabId=" + encodeURIComponent(sender.tab.id) + "&domain=" + encodeURIComponent(request.domain);
    chrome.experimental.infobars.show({
      tabId: sender.tab.id,
      path: url
    });
    return {};
  },
  calcEncryptedPassword: function(request, sender) {
    return { password: encryptPassword(request.domain, request.password, getOptions()) }
  },
  getDomainList: function(request, sender) {
    return {
      domainList: [ request.domain, "test1", "test2" ],
      recommendDomain: "test1"
    };
  }
};

function encryptPassword(domain, password, options) {
  var str = password + "\t" + domain;
  var digestBytes = Crypto.SHA256(password, { asBytes: true });
  var digestString = Crypto.util.bytesToBase64(digestBytes);
  digestString = digestString.substr(0, options.pswLength);
  return digestString;
}

function getOptions() {
  var options = {};
  options.pswLength = localStorage['pswLength'] || defaultOptions.PSW_LENGTH;
  return options;
}

    </script>
  </body>
</html>
